package com.nuanshui.heatedloan.fund.integration.pay.execute.sysexecute;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.heatedloan.constant.FundActionState;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.PayType;
import com.nuanshui.heatedloan.fund.constant.AccountBillStatus;
import com.nuanshui.heatedloan.fund.facade.UserAccountBillService;
import com.nuanshui.heatedloan.fund.facade.UserFundActionService;
import com.nuanshui.heatedloan.fund.integration.form.PayForm;
import com.nuanshui.heatedloan.fund.integration.pay.annotation.PayHandlerComponent;
import com.nuanshui.heatedloan.fund.integration.pay.execute.PayConfirmHandler;
import com.nuanshui.heatedloan.fund.model.PayResult;
import com.nuanshui.heatedloan.fund.model.UserAccountBill;
import com.nuanshui.heatedloan.fund.model.UserFundAction;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

/**
 * 生成用户账单处理逻辑
 * @param 
 * @author guohao
 * @date 2017年1月10日 上午11:45:33
 * @since $
 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@PayHandlerComponent(order=200,value="userAccountBillPayConfirmHandler")
public class UserAccountBillPayConfirmHandler implements PayConfirmHandler {

	private PayConfirmHandler payConfirmHandler;
	
	@Reference(url=GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.FUND_DUBBO_SERVER_VERSION)
	private UserAccountBillService userAccountBillService;

	@Reference(url=GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.FUND_DUBBO_SERVER_VERSION)
	private UserFundActionService userFundActionService;
	
	@Override
	public PayResult executePaymentConfirm(PayForm payForm) {
		
		PayResult payResult = payConfirmHandler.executePaymentConfirm(payForm);
		if(payResult.isSuccess()) {//表示处理成功，生成对象的账单			
			UserAccountBill userAccountBill = generateBill( payResult,payForm);
			if(payForm.getPayType().equals(PayType.YE)) {
				deductWallet(payResult, payForm, userAccountBill);
			}
		}
		return payResult;
	}
	
	
	public UserAccountBill generateBill(PayResult payResult,PayForm payForm) {
		// 插入账单表
		UserAccountBill userAccountBill = new UserAccountBill();
		userAccountBill.setUserId(payResult.getUserId());
		userAccountBill.setDealNum(payForm.getOrderNum());
		userAccountBill.setMoney((long) (payForm.getPayPrice() * -1));
		userAccountBill.setName(payResult.getTitle());
		userAccountBill.setStatus(AccountBillStatus.COMPLETE);
		userAccountBill.setBizId(payResult.getBizId());
		if(StringUtils.isNotEmpty(payResult.getBillType())){
			userAccountBill.setType(payResult.getBillType());
		}else {
			userAccountBill.setType(payForm.getPayBizType());
		}
		userAccountBill.setPlatform(payForm.getPayType());
		userAccountBill.setSubType(payResult.getSubType());
		return userAccountBillService.insertBillRecord(userAccountBill);
	}
	
	/**
	 * 扣除钱包的钱
	 * 
	 * @param
	 * @author guohao
	 * @date 2016年3月7日 下午8:53:00
	 */
	private void deductWallet(PayResult payResult,PayForm payForm, UserAccountBill userAccountBill) {

		// 插入余额表
		UserFundAction userFundAction = new UserFundAction();
		userFundAction.setActionType(payForm.getPayBizType());
		userFundAction.setStatus(FundActionState.COMPLETE);
		userFundAction.setUserId(payResult.getUserId());
		if(payResult.getPrice()!=null){
			userFundAction.setMoney(payResult.getPrice());
		}else {
			userFundAction.setMoney((long) (payForm.getPayPrice() * -1));	
		}	
		userFundAction.setPlatform(payForm.getPayType());
		userFundAction.setDealNum(payForm.getOrderNum());
		userFundAction.setBizId(userAccountBill == null ? null :userAccountBill.getId());
		userFundActionService.payGoodsMoney(userFundAction);
	}
	
	@Override
	public void setPayConfirmHandler(PayConfirmHandler payConfirmHandler) {

		this.payConfirmHandler = payConfirmHandler;
	}
	
}
